-
-
Notifications
You must be signed in to change notification settings - Fork 626
chore(iterators): create Iterator module and migrate iterators to use it #1392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic.
- NOP case
Iterator.new(nodes):consume()
tested ok. Iterator.new(nodes)
fails but I don't think that matters.- Tested expand-all as it is a big change
- Reviewed other usages but they are all trivial refactors and did not test
lua/nvim-tree/iterator.lua
Outdated
local Iterator = {} | ||
Iterator.__index = Iterator | ||
|
||
function Iterator.new(nodes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really a NodeIterator
; we can call it that. More iterators can be added in the future, including a generic one.
All functions except match and apply are node specific. The could go in an AbstractIterator
parent class, but that can come when more iterators are added.
lua/nvim-tree/actions/expand-all.lua
Outdated
if node.open then | ||
if iterate(node) then | ||
return true | ||
Iterator.new(parent.nodes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love the fluid nature. This is classic builder pattern. Perhaps we could make the naming more concise e.g.
NodeIterator.builder(parent.nodes)
:hidden()
:matcher(...)
:applier(...)
:recursor(...)
:build()()
The :build()()
is a bit redundant and implies that we have an intermediate NodeIteratorBuilder
which doesn't add much value, so :consume()
or iterate()
is appropriate.
lua/nvim-tree/iterator.lua
Outdated
end | ||
|
||
function Iterator:allow_hidden() | ||
self._filter_hidden = function(_) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/nit this could just be a boolean member.
b17b22f
to
10aa799
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beautiful
Missing one iterator (see TODO comment), because i'm not sure about the interface. This is a first attempt to clean up the repetitive code for iterators.
Not sure this is the best implementation i could go with, but it works for now.